﻿using HC.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace HC.View
{
    public partial class HCView
    {
        /// <summary>
        /// 将文档每一页保存为图片
        /// </summary>
        /// <param name="aPath">图片路径</param>
        /// <param name="aPrefix">图片名称前缀</param>
        /// <param name="aImageType">图片格式如 BMP, JPG, PNG</param>
        public string SaveToImageBase64(string aPrefix, string aImageType, bool aOnePaper = true)
        {
            HCCanvas vBmpCanvas = new HCCanvas();
            SectionPaintInfo vPaintInfo = new SectionPaintInfo();
            try
            {
                vPaintInfo.ScaleX = 1;
                vPaintInfo.ScaleY = 1;
                vPaintInfo.Zoom = 1;
                vPaintInfo.Print = true;
                vPaintInfo.DPI = HCUnitConversion.PixelsPerInchX;
                vPaintInfo.ViewModel = HCViewModel.hvmFilm;

                int vWidth = 0, vHeight = 0;
                for (int i = 0; i < FSections.Count; i++)
                {
                    vHeight = vHeight + FSections[i].PaperHeightPix * FSections[i].PageCount;
                    if (vWidth < FSections[i].PaperWidthPix)
                        vWidth = FSections[i].PaperWidthPix;
                }

                vPaintInfo.WindowWidth = vWidth;
                vPaintInfo.WindowHeight = vHeight;

                using (Bitmap vBmp = new Bitmap(vWidth, vHeight))
                {
                    vBmpCanvas.Graphics = Graphics.FromImage(vBmp);

                    int vSectionIndex = 0, vSectionPageIndex = 0, vTop = 0;
                    for (int i = 0; i < this.PageCount; i++)
                    {
                        vSectionIndex = GetSectionPageIndexByPageIndex(i, ref vSectionPageIndex);
                        //vWidth = FSections[vSectionIndex].PaperWidthPix;
                        vHeight = FSections[vSectionIndex].PaperHeightPix;

                        vBmpCanvas.Brush.Color = Color.White;
                        vBmpCanvas.FillRect(new RECT(0, vTop, vWidth, vTop + vHeight));

                        ScaleInfo vScaleInfo = vPaintInfo.ScaleCanvas(vBmpCanvas);
                        try
                        {
                            FSections[vSectionIndex].PaintPaper(vSectionPageIndex, 0, vTop, vBmpCanvas, vPaintInfo);
                            vTop = vTop + vHeight;
                        }
                        finally
                        {
                            vPaintInfo.RestoreCanvasScale(vBmpCanvas, vScaleInfo);
                        }
                    }


                    String strbaser64 = "";
                    if (aImageType == "BMP")
                        strbaser64 = HC.GraphicToBase64(vBmp, System.Drawing.Imaging.ImageFormat.Bmp);
                    else
                    if (aImageType == "JPG")
                        strbaser64 = HC.GraphicToBase64(vBmp, System.Drawing.Imaging.ImageFormat.Jpeg);
                    else
                        strbaser64 = HC.GraphicToBase64(vBmp, System.Drawing.Imaging.ImageFormat.Png);

                    return strbaser64;

                }
            }
            finally
            {
                vPaintInfo.Dispose();
            }
        }


        public bool LoadHeaderFromXmlStream(Stream stream)
        {
            bool vResult = false;
            this.BeginUpdate();
            try
            {
                FUndoList.Clear();
                FUndoList.SaveState();
                try
                {
                    FUndoList.Enable = false;

                    XmlDocument vXml = new XmlDocument();
                    vXml.PreserveWhitespace = true;
                    vXml.Load(stream);
                    if (vXml.DocumentElement.Name == "HCView")
                    {
                        if (vXml.DocumentElement.Attributes["EXT"].Value != HC.HC_EXT)
                            return false;

                        string vVersion = vXml.DocumentElement.Attributes["ver"].Value;
                        byte vLang = byte.Parse(vXml.DocumentElement.Attributes["lang"].Value);

                        FStyle.States.Include(HCState.hosLoading);
                        try
                        {
                            XmlElement vNode = null;
                            HCStyle style = new HCStyle();
                            for (int i = 0; i < vXml.DocumentElement.ChildNodes.Count; i++)
                            {
                                vNode = vXml.DocumentElement.ChildNodes[i] as XmlElement;
                                if (vNode.Name == "style")
                                    style.ParseXml(vNode);
                                else
                                if (vNode.Name == "sections")
                                {
                                    HCSection vSection = new HCSection(style);
                                    vSection.ParseXml(vNode.ChildNodes[0] as XmlElement);
                                    using (Stream Xstream = new MemoryStream())
                                    {
                                        vSection.Header.SaveToStream(Xstream);
                                        Xstream.Seek(0, SeekOrigin.Begin);
                                        this.ActiveSection.Header.LoadFromStream(Xstream, style, HC.HC_FileVersionInt);
                                    }
                                }
                            }

                        }
                        finally
                        {
                            FStyle.States.Exclude(HCState.hosLoading);
                        }

                    }
                }
                finally
                {
                    FUndoList.RestoreState();
                }
            }
            finally
            {
                this.EndUpdate();
            }

            return vResult;
        }

        public bool LoadFooterFromXmlStream(Stream stream)
        {
            bool vResult = false;
            this.BeginUpdate();
            try
            {
                FUndoList.Clear();
                FUndoList.SaveState();
                try
                {
                    FUndoList.Enable = false;

                    XmlDocument vXml = new XmlDocument();
                    vXml.PreserveWhitespace = true;
                    vXml.Load(stream);
                    if (vXml.DocumentElement.Name == "HCView")
                    {
                        if (vXml.DocumentElement.Attributes["EXT"].Value != HC.HC_EXT)
                            return false;

                        string vVersion = vXml.DocumentElement.Attributes["ver"].Value;
                        byte vLang = byte.Parse(vXml.DocumentElement.Attributes["lang"].Value);

                        FStyle.States.Include(HCState.hosLoading);
                        try
                        {
                            XmlElement vNode = null;
                            HCStyle style = new HCStyle();
                            for (int i = 0; i < vXml.DocumentElement.ChildNodes.Count; i++)
                            {
                                vNode = vXml.DocumentElement.ChildNodes[i] as XmlElement;
                                if (vNode.Name == "style")
                                    style.ParseXml(vNode);
                                else
                                if (vNode.Name == "sections")
                                {
                                    HCSection vSection = new HCSection(style);
                                    vSection.ParseXml(vNode.ChildNodes[0] as XmlElement);
                                    using (Stream Xstream = new MemoryStream())
                                    {
                                        vSection.Footer.SaveToStream(Xstream);
                                        Xstream.Seek(0, SeekOrigin.Begin);
                                        this.ActiveSection.Footer.LoadFromStream(Xstream, style, HC.HC_FileVersionInt);
                                    }
                                }
                            }

                        }
                        finally
                        {
                            FStyle.States.Exclude(HCState.hosLoading);
                        }

                    }
                }
                finally
                {
                    FUndoList.RestoreState();
                }
            }
            finally
            {
                this.EndUpdate();
            }

            return vResult;
        }
    }
}
